// const { execSync } = require("child_process");
// const chalk = require("chalk");
const path = require("path");
const fs = require("fs-extra");
const simpleGit = require("simple-git");
const git = simpleGit();

// 判断当前文件夹是否gitinit
const isGitInit = () => {
  try {
    let result = git.checkIsRepo();
    return result;
  } catch (error) {
    console.log(error, "errorddd");
  }
};

// 初始化git,并添加远端地址
const gitInit = async (remote) => {
  try {
    let result = await git.init();
    if (result) {
      await git.addRemote("origin", remote);
      //生成一个readme文件
      fs.writeFileSync(path.join(process.cwd(), "README.md"), "# README");
      await git.add(".");
      await git.commit("first commit");
      await git.push("origin", "master");
    }
    return result;
  } catch (error) {
    console.log(error);
    // console.log(chalk.red(error));
  }
};

/**同步获取修改或新增的文件或文件夹 */
const getFilesChanged = async () => {
  let newFiles = [];
  let modifiedFiles = [];
  let deletedFiles = [];
  //   冲突的文件
  let conflictedFiles = [];
  try {
    let result = await git.status();
    if (result) {
      newFiles = result.not_added;
      modifiedFiles = result.modified;
      deletedFiles = result.deleted;
      conflictedFiles = result.conflicted;
    }
    return {
      newFiles,
      modifiedFiles,
      deletedFiles,
      conflictedFiles,
    };
  } catch (error) {
    console.log(error);
    // console.log(chalk.red(error));
  }
};

/**获取分支信息 */
const getBrachInfo = async () => {
  let branchList = [];
  let currentBranch = "";
  let b = null;
  try {
    b = await git.branchLocal();
    branchList = b.all;
    currentBranch = b.current;
    return {
      branchList,
      currentBranch,
    };
  } catch (error) {
    // console.log(chalk.red(error));
  }
};

// 选择文件并提交q
const handleAdd = async (files) => {
  console.log(files, "fileStr");
  try {
    let result = await git.add(files);
    return result;
  } catch (error) {
    console.log(error);
    // console.log(chalk.red(error));
  }
};

// 全部提交
const handleAddAll = async () => {
  try {
    let result = await git.add(".");
    return result;
  } catch (error) {
    // console.log(chalk.red(error));
  }
};

// 提交信息
const handleCommit = (message, cb) => {
  try {
    cb();
    let result = git.commit(message);
    return result;
  } catch (error) {
    // console.log(chalk.red(error));
  }
};

// 合并分支
const handleMerge = (branch) => {
  console.log(branch, "branch");
  try {
    let result = git.merge([branch]);
    return result;
  } catch (error) {
    console.log(error);
    // console.log(chalk.red(error));
  }
};

// 切换分支
const handleCheckout = (branch) => {
  try {
    let result = git.checkout(branch);
    return result;
  } catch (error) {
    // console.log(chalk.red(error));
  }
};

// 提交
const handlePush = async () => {
  try {
    let { currentBranch } = await getBrachInfo();
    let result = git.push("origin", currentBranch);
    return result;
  } catch (error) {
    console.log(error);
  }
};

// 删除当前git地址
const handleRemoveRemote = async () => {
  try {
    // 判断.git文件夹路径 存在不存在
    const stat = fs.statSync(path.join(process.cwd(), ".git"));
    let isGit = stat.isDirectory();
    console.log(isGit, "isGit");
    if (!isGit) {
      return;
    }

    const gitDir = path.join(process.cwd(), ".git");
    console.log(gitDir, "gitDir");
    // fs-extra删除文件夹
    fs.removeSync(gitDir);
    // let result = git.removeRemote("origin");
    // return res
  } catch (error) {
    console.log(error);
    // console.log(chalk.red(error));
  }
};

const copyFile = (origin, target) => {
  // // 执行cp命令复制文件
  // const { execSync } = require("child_process");
  // try {
  //   execSync(`cp -r ./src/template/index.ejs ${process.cwd()}/.husky/index.js`);
  // } catch (error) {
  //   // console.log(chalk.red(error));
  // }
  // 通过fs-extra复制文件
  try {
    fs.copySync(origin, target);
  } catch (error) {
    console.log(error);
    // console.log(chalk.red(error));
  }
};

// const init = async () => {
//   handleRemoveRemote();
// };
// init();

module.exports = {
  getFilesChanged,
  getBrachInfo,
  handleAdd,
  handleAddAll,
  handleCommit,
  handleMerge,
  handleCheckout,
  handlePush,
  isGitInit,
  gitInit,
  copyFile,
  handleRemoveRemote,
};
